// pages/play/play.js
var config = require('../../utils/config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPlaying: false,
    song: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    var songid = options.songid;//获取页面传过来的歌曲id
    if (songid === undefined) {
      var curSong = wx.getStorageSync('curSong') || {};
      if (curSong === undefined) {
        var song = { songname: '未选择歌曲' };
        self.setData({
          song: song
        })
      } else {
        self.setData({
          song: curSong
        })
      }
    } else {
      var songlist = wx.getStorageSync('songlist') || [];
      for (var i = 0; i < songlist.length; i++) {
        if (songlist[i].songid == songid) {
          self.setData({
            song: songlist[i]
          });
          break;
        }
      }
    }
    wx.setStorageSync('curSong', self.data.song);
  },
  playToggle: function () {
    console.log('进来了');
    var self = this;
    if (this.data.song.songname == '未选择歌曲') {
      return;
    }
    if (self.data.isPlaying) {
      wx.stopBackgroundAudio();
    } else {
      console.log(self.data.song.url);
      wx.playBackgroundAudio({
        dataUrl: 'http://124.14.20.12/amobile.music.tc.qq.com/C4000042ITLD1isg7z.m4a?guid=2486414864&vkey=2BEEE505C8CD3211AE1242CB4424099F077FEED081738FA2904AE2B6B78B39CDC3D9BBA8B3666AA2C24ADFF0311ED379065B16D661A27D9B&uin=6271&fromtag=66',
        success: function (res) {
          //console.log('播放成功')
        }
      })
    }
    self.setData({
      isPlaying: !this.data.isPlaying
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})